﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Tomahawk.Runtime.Logic.UI;
using Tomahawk.Runtime.Canvas.Sprite;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Tomahawk.Runtime.Timeline
{
    public class DisappearEffectTrack : SpriteEffectTrack
    {
        [XmlElement("OverlayAlpha")]
        public string overlayAlpha = "";

        [XmlElement("OverlayX")]
        public string overlayX = "";

        [XmlElement("OverlayY")]
        public string overlayY = "";

        private float alpha = 1.0f;
        private Vector2 overlay = Vector2.Zero;
        private Curve curveOverlayAlpha = new Curve();
        private Curve curveOverlayX = new Curve();
        private Curve curveOverlayY = new Curve();

        public DisappearEffectTrack() : base() { }

        public DisappearEffectTrack(string name) : base(name) { }

        protected override string GetEffectFile()
        {
            return @"Tomahawk.Content\SpriteEffects\DisappearEffect";
        }

        protected override void OnLoadEffectParameters(Effect effect)
        {
            effect.Parameters["OverlayAlpha"].SetValue(alpha);
            effect.Parameters["OverlayScroll"].SetValue(overlay);
        }

        protected override bool OnLoad(GraphicsDevice graphics, ContentManager contentManager)
        {
            curveOverlayAlpha = LoadCurve(contentManager, overlayAlpha);
            curveOverlayX = LoadCurve(contentManager, overlayX);
            curveOverlayY = LoadCurve(contentManager, overlayY);

            return base.OnLoad(graphics, contentManager);
        }

        public override void SortKeys() { }

        public override bool Update(float time)
        {
            if (curveOverlayAlpha == null || curveOverlayX == null || curveOverlayY == null)
            {
                return false;
            }

            alpha = curveOverlayAlpha.Evaluate(time);
            overlay.X = curveOverlayX.Evaluate(time);
            overlay.Y = curveOverlayY.Evaluate(time);

            return true;
        }
    }
}
